<template>
  <div :id="id" ref="boxwrap" :class="className" :style="{height:height,width:width}" :title="title" />
</template>

<script>
import resize from '@/components/Charts/mixins/resize'

export default {
  mixins: [resize],
  props: {
    className: {
      type: String,
      default: 'chart'
    },
    id: {
      type: String,
      default: 'chart'
    },
    width: {
      type: String,
      default: '200px'
    },
    height: {
      type: String,
      default: '200px'
    },
    title: {
      type: String,
      default: ''
    }
  },
  data() {
    return {
      chart: null,
      fontColor: '#bebebe',
      color: ['rgba(46,199,201,1)', 'rgba(255,185,128,1)']
    }
  },
  watch: {
    title: {
      immediate: true,
      handler: function(newval, oldval) {
        this.title = newval
        console.log(this.title)
        if (this.chart) {
          this.chart.dispose()
          this.chart = null
          this.initChart()
        }
      },
      deep: true
    }
  },
  mounted() {
    this.domResize()
    this.initChart()
  },
  beforeDestroy() {
    if (!this.chart) {
      return
    }
    this.chart.dispose()
    this.chart = null
  },
  methods: {
    initChart() {
      const peopleObj = {
        '应到人数': 80,
        '实到人数': 75
      }
      this.chart = this.$echarts.init(document.getElementById(this.id))

      this.chart.setOption({
        color: this.color,
        title: {
          text: this.title,
          top: '1%',
          left: 'center',
          textStyle: {
            fontSize: 16,
            color: this.fontColor
          }
        },
        tooltip: {
          trigger: 'axis'
        },
        legend: {
          top: '1%',
          right: '1%',
          textStyle: {
            fontSize: 14,
            color: this.fontColor
          },
          formatter: function(name) {
            return name + peopleObj[name] + '人'
          }
        },
        grid: {
          top: '20%',
          left: '5%',
          right: '5%',
          bottom: '15%',
          containLabel: true
        },
        // dataZoom: [{
        //   show: true,
        //   type: 'inside',
        //   bottom: 15,
        //   height: 10,
        //   startValue: 0,
        //   endValue: 5
        // }, {
        //   show: true,
        //   bottom: 15,
        //   height: 10,
        //   startValue: 0,
        //   endValue: 5
        // }],
        xAxis: [{
          type: 'category',
          boundaryGap: false,
          axisLabel: {
            color: this.fontColor
          },
          data: ['11/1', '11/2', '11/3', '11/4', '11/5', '11/6', '11/7', '11/8', '11/9', '11/10', '11/11', '11/12']
        }],
        yAxis: [{
          type: 'value',
          name: '',
          nameTextStyle: {
            color: this.fontColor
          },
          axisTick: {
            show: false
          },
          axisLabel: {
            color: this.fontColor
          },
          splitLine: {
            lineStyle: {
              color: 'rgba(255,255,255,0.1)'
            }
          }
        }],
        series: [{
          name: '应到人数',
          type: 'line',
          smooth: true,
          label: {
            show: true,
            position: 'top',
            color: this.fontColor
          },
          areaStyle: {
            color: {
              type: 'linear', x: 0, y: 0, x2: 0, y2: 1,
              colorStops: [{
                offset: 0, color: 'rgba(46,199,201,1)' // 0% 处的颜色
              }, {
                offset: 1, color: 'rgba(46,199,201, 0)' // 100% 处的颜色
              }],
              global: false // 缺省为 false
            }
          },
          data: [12, 13, 10, 13, 9, 3, 10, 10, 19, 15, 8, 9]
        }, {
          name: '实到人数',
          type: 'line',
          smooth: true,
          label: {
            show: true,
            position: 'top',
            color: this.fontColor
          },
          areaStyle: {
            color: {
              type: 'linear', x: 0, y: 0, x2: 0, y2: 1,
              colorStops: [{
                offset: 0, color: 'rgba(255,185,128,1)' // 0% 处的颜色
              }, {
                offset: 1, color: 'rgba(255,185,128, 0)' // 100% 处的颜色
              }],
              global: false // 缺省为 false
            }
          },
          data: [12, 8, 9, 13, 9, 3, 10, 8, 14, 9, 8, 9]
        }]
      })
    },
    domResize() {
      const that = this
      const dom = this.$refs.boxwrap
      this.$erd.listenTo(dom, function(element) {
        that.chart.resize()
      })
    }
  }
}
</script>
